package com.example.mqspring.rabiitmqbasicleverone.unroutable;

import com.example.mqspring.utils.RabbitMqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Producer {

    static private String message = "i want some money";
//   强制标志
    static private boolean ismandatory = true;

    public void configChannel(Channel channel) throws Exception {
//      添加返回监听器
        channel.addReturnListener(new HandlingReturnListener());
//       定义交换器
        channel.exchangeDeclare(RabbitMqUtil.exchange,"direct",false,false,null);
    }

    public static void main(String[] args) {
        Producer producer = new Producer();
        Connection connection = RabbitMqUtil.createConnection();
        Channel channel = null;
        try {
            channel = connection.createChannel();
            producer.configChannel(channel);
            //发送消息
            channel.basicPublish(RabbitMqUtil.exchange,"",ismandatory,null,message.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
